import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_wan_android/arch/net/http_manager.dart';
import 'package:flutter_wan_android/arch/net/result_data.dart';
import 'package:flutter_wan_android/arch/ui/toast.dart';
import 'package:flutter_wan_android/event/event.dart';
import 'package:flutter_wan_android/event/event_bus.dart';
import 'package:flutter_wan_android/model/article.dart';
import 'package:flutter_wan_android/res/theme.dart';
import 'package:flutter_wan_android/routes/route_util.dart';
import 'package:flutter_wan_android/routes/routes.dart';
import 'package:flutter_wan_android/widget/article_item.dart';
import 'package:flutter_wan_android/widget/refresh.dart';
import 'package:flutter_wan_android/widget/wan_appbar.dart';
import 'package:fluttertoast/fluttertoast.dart';

class QAPage extends StatefulWidget {
  @override
  _QAPageState createState() => _QAPageState();
}

class _QAPageState extends State<QAPage> {
  List<Article> _articles = [];

  EasyRefreshController _controller = EasyRefreshController();

  var _pageIndex = 0;

  @override
  void initState() {
    super.initState();
    _getQaData();

    EventBusUtil.getInstance().on<String>().listen((event) {
      if(event == WEvent.EVENT_LOGIN){
        _pageIndex = 0;
        _getQaData();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    EventBusUtil.getInstance().destroy();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WanAppbar(context, titleStr: "问答", actions: [
        IconButton(
          icon: Icon(Icons.search),
          onPressed: () {
            RouteUtil.push(context, Routes.search);
          },
        )
      ]),
      body: Container(
        color: wTheme.background,
        child: EasyRefresh(
          controller: _controller,
          enableControlFinishLoad: true,
          firstRefresh: true,
          enableControlFinishRefresh: true,
          header: refreshHeader,
          footer: refreshFooter,
          child: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              return ArticleItem(
                _articles[index],
                onRefresh: () {
                  setState(() {
                    List<Article> newList = [];
                    newList.addAll(_articles);
                    newList[index].collect = !(newList[index].collect ?? false);
                    _articles = newList;
                  });
                },
              );
            },
            itemCount: _articles.length,
          ),
          onRefresh: () async {
            _pageIndex = 0;
            _getQaData();
          },
          onLoad: () async {
            _getQaData();
          },
        ),
      ),
    );
  }

  _getQaData() async {
    ResultData result = await HttpManager.instance()
        .get("wenda/list/$_pageIndex/json", withLoading: false);

    _controller.finishRefresh();
    bind(_controller, result);

    if (result.success) {
      setState(() {
        if (_pageIndex == 0) {
          _articles = getArticleList(result.data["datas"]);
        } else {
          _articles.addAll(getArticleList(result.data["datas"]));
        }
      });
      _pageIndex++;
    } else {
      Fluttertoast.showToast(msg: result.msg);
    }
  }
}
